package mp3model;

import java.io.File;
import java.io.IOException;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.CannotWriteException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.FieldDataInvalidException;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.KeyNotFoundException;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.TagField;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.AbstractTag;
import org.jaudiotagger.tag.id3.ID3v1Tag;
import org.jaudiotagger.tag.id3.ID3v23Tag;
import org.jaudiotagger.tag.id3.ID3v24Tag;

public class Mp3Model {

	private MP3File mp3File;
	private AbstractID3v2Tag v23Tag;
	private boolean isV2Tag;
	private ID3v1Tag tag;
	
	public Mp3Model(File file) {
		try {
			mp3File = (MP3File) AudioFileIO.read( file);
		} catch (CannotReadException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TagException e) {
			e.printStackTrace();
		} catch (ReadOnlyFileException e) {
			e.printStackTrace();
		} catch (InvalidAudioFrameException e) {
			e.printStackTrace();
		}
		tag = mp3File.hasID3v1Tag() ? mp3File.getID3v1Tag() : new ID3v1Tag();
		v23Tag = mp3File.getID3v2Tag();
		isV2Tag = mp3File.hasID3v2Tag();
	}
	
	public boolean isV2Tag() {
		return isV2Tag;
	}
	
	public Mp3Model(String path) {
		this( new File(path));
	}
	
	private String getField(FieldKey key) {
		return v23Tag.getFirst( key);
	}
	
	private void setField( FieldKey key, String value) {
		try {
			if (mp3File.hasID3v1Tag()) {
				tag.setField(key, value);
				mp3File.setID3v1Tag(tag);
			}

			if (mp3File.hasID3v2Tag()) {
				v23Tag.setField(key, value);
				mp3File.setID3v2Tag(v23Tag);
			}
		} catch (KeyNotFoundException e1) {
			e1.printStackTrace();
		} catch (FieldDataInvalidException e1) {
			e1.printStackTrace();
		}
	}
	
	public String getArtist() {
		return v23Tag.getFirst(FieldKey.ARTIST);
	}
	
	public void setArtist(String artist) {
		setField( FieldKey.ARTIST, artist);
	}
	
	
	public String getTitle() {
		return getField( FieldKey.TITLE);
	}
	
	public void setTitle(String title) {
		setField(FieldKey.TITLE, title);
	}
	
	public String getAlbum() {
		return getField( FieldKey.ALBUM);
	}
	
	public void setAlbum(String value) {
		setField(FieldKey.ALBUM, value);
	}
	
	public String getFilename() {
		return mp3File.getFile().getName();
	}
	
	public void setFilename( String filename) {
		try {
			File file = new File(filename);
			if ( !file.exists())
				file.createNewFile();
			mp3File.save(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void save() {
		try {
			mp3File.save();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TagException e) {
			e.printStackTrace();
		}
	}
	
}

